package gob.pe.mtpe.struts.action;

import gob.pe.mtpe.ibatis.despachadores.SeguroDespatch;
import gob.pe.mtpe.struts.form.SeguroVidaForm;
import gob.pe.mtpe.util.funciones.Funciones;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import org.apache.commons.fileupload.FileUploadException;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;



public class UpLoadAction extends DispatchAction implements Serializable{
    private static Logger depurador = Logger.getLogger(UpLoadAction.class.getName());
    private Funciones funciones;
    public ActionForward remplazarArchivo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, FileUploadException {
 	 String v_path  = Funciones.ObtieneProperty("mtpe.util.properties.mtpe_reportes","R22");
    	 String archivo = request.getParameter("v_nomarch");
         SeguroVidaForm cForm = (SeguroVidaForm)form;
         
         FormFile myFile=null;
       try{ File fdelete = new File(v_path + archivo);
             fdelete.delete();
                         
             myFile = cForm.getFichero();
            
             if(myFile.getFileSize()<512000){
	             String fileName    = myFile.getFileName();
	             if(!fileName.equals("")){
	            	 File fileDeleteFileName = new File(v_path + fileName);
	            	if(fileDeleteFileName.exists())
	                	fileDeleteFileName.delete();
	                File fileToCreate = new File(v_path, fileName);
	                if(fileToCreate.exists())
	                	fileToCreate.delete();
	                if(!fileToCreate.exists()){
	                    FileOutputStream fileOutStream = new FileOutputStream(fileToCreate);                   
	                    fileOutStream.write(myFile.getFileData());                    
	                    fileOutStream.flush();
	                    fileOutStream.close();                   
	                    File fcreate = new File(v_path + fileName);
	                    File frename = new File(v_path + archivo);
	                    boolean correcto = fcreate.renameTo(frename);
	                   if (correcto)
	                      depurador.info("El renombrado ha sido correcto");
	                    else
	                      depurador.info("El renombrado no se ha podido realizar");
	                } 
	             } 
	           
	             request.setAttribute("exito", "1");
		         request.setAttribute("busqueda", "0");
	             request.setAttribute("v_nomarch", archivo);	             
	             request.setAttribute("v_tamanio", "false");
	            
            }else{
            	request.setAttribute("exito", "1");
            	request.setAttribute("busqueda", "0");
            	request.setAttribute("v_nomarch", archivo);
            	request.setAttribute("v_tamanio", "true");
            }
            return new ActionForward("/jspDocs/modulos/SeguroVida/remplazarArchivo.jsp");

        } catch(Exception e){
             Funciones funciones=new Funciones();
            depurador.log(Level.ERROR,"   "+myFile.getFileSize()+"  EMPRESA "+cForm.getV_ruc()+ "ERROR "+funciones.readStackTrace(e));
             return new ActionForward("/jspDocs/errores/error.jsp"); 
        }
         
    }
   
       
}
